package com.boarsoft.boar.message.service.impl;

import com.boarsoft.boar.message.service.MsgClientService;
import com.boarsoft.message.bean.Message;
import com.boarsoft.message.bean.MessageEntity;
import com.boarsoft.message.bean.MessageQueryParam;
import com.boarsoft.message.broker.MessageBroker;
import com.boarsoft.message.core.Messenger;
import com.boarsoft.rpc.core.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
@Component("msgClientSvc")
public class MsgClientServiceImpl implements MsgClientService {
	private static final Logger log = LoggerFactory.getLogger(MsgServerServiceImpl.class);

	@Autowired
	@Qualifier("msgBroker")
	private MessageBroker broker;
	@Override
	public List<MessageEntity> list(String target, String addr, String messageId, Date startTime, Date endTime, Short status, String key, int pageNo, int pageSize) {
		RpcContext.specify2(addr);
		try {
			MessageQueryParam param = new MessageQueryParam();
			param.setId(messageId);
			param.setKey(key);
			param.setEndTime(endTime);
			param.setStartTime(startTime);
			param.setStatus(status);
			param.setPageNo(pageNo);
			param.setPageSize(pageSize);
			return broker.query(target, param);
		} catch (Exception e) {
			log.error("query message error:", e);
			return null;
		} finally {
			RpcContext.specify2(null);
		}
	}

	@Override
	public boolean update(Message message) {
		return true;

	}

	@Override
	public boolean resend(Message message) {
		//先修改消息
		update(message);
		try {
			Messenger.send(message);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return true;
	}



}
